﻿using BCCommon;
using BCData.Common.Banner;
using BCDto.Common.Banner;
using BCEntity.Common.Banner;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Text;

namespace BCService.Common.Banner
{
    /// <summary>
    /// Banner分类编码服务
    /// </summary>
    public class BannerTagcodeService : IBannerTagcodeService
    {
        private readonly IRedisService redisService;
        private readonly IBannerData bannerData;
        private readonly IBannerTagcodeData bannerTagcodeData;
        private readonly string key = string.Format("{0}", EntityNameConst.BannerTagcodeEntity);
        public BannerTagcodeService(IRedisService redisService, IBannerTagcodeData bannerTagcodeData, IBannerData bannerData)
        {
            this.redisService = redisService;
            this.bannerTagcodeData = bannerTagcodeData;
            this.bannerData = bannerData;
        }

        #region 检查Banner分类编码是否存在
        public bool Exists(string bannerTagcode)
        {
            return this.bannerTagcodeData.Exists(bannerTagcode).GetAwaiter().GetResult();
        }
        #endregion

        public BannerTagcodeDto Add(BannerTagcodeRequestDto bannerTagcodeRequestDto)
        {
            if (this.bannerTagcodeData.CheckBannerTagcodeExists(bannerTagcodeRequestDto.BannerTagcode))
            {
                throw new ArgumentException("Banner分类编码已存在。");
            }
            return this.bannerTagcodeData.Add(bannerTagcodeRequestDto.As<BannerTagcodeEntity>()).As<BannerTagcodeDto>();
        }

        public void Delete(string bannerTagcode)
        {
            if (!this.Exists(bannerTagcode))
            {
                throw new ArgumentException("此记录不存在!");
            }

            if (this.bannerData.CheckBannerTagcodeInUse(bannerTagcode))
            {
                throw new ArgumentException("Banner分类编码正在使用中，无法删除。");
            }

            this.bannerTagcodeData.Delete(bannerTagcode);
            string cacheKey = this.redisService.GetKey(key, bannerTagcode);
            this.redisService.DeleteAsync(cacheKey);
        }

        public BannerTagcodeDto Get(string bannerTagcode)
        {
            string cacheKey = this.redisService.GetKey(key, bannerTagcode);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.bannerTagcodeData.Get(bannerTagcode);
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<BannerTagcodeDto>();
        }

        public IEnumerable<BannerTagcodeDto> GetList()
        {
            var result = this.redisService.TryGet(key, () =>
            {
                return this.bannerTagcodeData.GetList();
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<BannerTagcodeDto>>();
        }

        public PageableList<BannerTagcodeDto> GetPageableList(string tagCode, bool? ascending, int pageIndex = 1, int pageSize = 20)
        {
            IEnumerable<BannerTagcodeEntity> entities = this.bannerTagcodeData.GetPageableList(tagCode, out int dataCount, ascending, pageIndex, pageSize);
            return new PageableList<BannerTagcodeDto>
            {
                Count = dataCount,
                Items = entities.As<IEnumerable<BannerTagcodeDto>>()
            };
        }

        public BannerTagcodeDto Update(BannerTagcodeRequestDto bannerTagcodeRequestDto)
        {
            if (bannerTagcodeRequestDto.IsSystem)
            {
                throw new ArgumentException("系统标记码无法修改！");
            }
            if (!this.bannerTagcodeData.CheckBannerTagcodeExists(bannerTagcodeRequestDto.BannerTagcode))
            {
                throw new ArgumentException("Banner分类编码不存在。");
            }
            return this.bannerTagcodeData.Update(bannerTagcodeRequestDto.As<BannerTagcodeEntity>()).As<BannerTagcodeDto>();
        }
        #region 启用Banner分类编码
        public bool Enabled(string bannerTagcode)
        {
            if (!this.Exists(bannerTagcode))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.bannerTagcodeData.Enabled(bannerTagcode).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, bannerTagcode);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 禁用Banner分类编码
        public bool Disabled(string bannerTagcode)
        {
            if (!this.Exists(bannerTagcode))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.bannerTagcodeData.Disabled(bannerTagcode).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, bannerTagcode);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion
    }
}
